//
//  BookClassCollectionViewCell.m
//  JPWords
//
//  Created by 梁淑婷 on 2024/6/23.
//

#import "BookClassCollectionViewCell.h"

@interface BookClassCollectionViewCell()

@property(nonatomic,strong)UIImageView *bookImageView;
@property(nonatomic,strong)UIButton *selBtn;

@end

@implementation BookClassCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        [self createUI];
    }
    return self;
}

-(void)createUI{
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    __weak typeof(self)self_weak_ = self;
    
    self.bookImageView = [[UIImageView alloc]init];
    self.bookImageView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bookImageView];
    [self.bookImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@124);
    }];
    
    self.selBtn = [[UIButton alloc]init];
    [self.selBtn setImage:[UIImage imageNamed:@"book_unsel"] forState:UIControlStateNormal];
    [self.selBtn setImage:[UIImage imageNamed:@"book_sel"] forState:UIControlStateSelected];
    self.selBtn.titleLabel.font = JPUIFontPingFangSCMedium(14);
    [self.selBtn setTitleColor:KFontBlackColor forState:UIControlStateNormal];
    [self.selBtn addTarget:self action:@selector(selBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.selBtn];
    [self.selBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(@0);
        make.height.mas_equalTo(@20);
        make.width.mas_equalTo(@20);
        make.centerX.mas_equalTo(self_weak_.contentView);
    }];
    
}


-(void)selBtnClick{
    self.selBtn.selected = !self.selBtn.selected;
    if(_selBlock){
        _selBlock(self.selBtn.selected);
    }
}

-(void)setModel:(BookListModel *)model{
    _model = model;
    
    [self.bookImageView sd_setImageWithURL:[NSURL URLWithString:model.path]];
    [self.selBtn setTitle:[NSString stringWithFormat:@" %@",model.volumeName] forState:UIControlStateNormal];
    self.selBtn.selected = model.isSel;
    CGFloat s_w = [self getStringWidthWithText:self.selBtn.titleLabel.text font:self.selBtn.titleLabel.font viewHeight:20];
    [self.selBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(s_w+20);
    }];
    
    
}

@end
